﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using HTspcAppProject.Model;
using System.IO;
using System.Configuration;
using HTDAL.HTSPC_DBDataSetTableAdapters;

namespace HTspcAppProject
{
    public class CommonClass
    {
        //modbus通道字典—key:通道编号（CH）
        public static Dictionary<int, string[]> chDict;
        //设备列表
        public static DataTable devDtList = new DataTable();

        public static int devCount;
        //设备详细信息字典-key:设备名（DevNm）
        public static Dictionary<string, ht_pmmr> pmDict;
        //设备概览信息字典-key:设备名（DevNm）
        public static Dictionary<string, ht_stmr> stDict;

        //public static Dictionary<string, decimal> CntTmpDict;



        public static int Jumpt = Convert.ToInt32(ConfigurationManager.AppSettings["JumpT"]);
        public static int Jumptc = Convert.ToInt32(ConfigurationManager.AppSettings["JumpTC"]);

        public static string mTile = Convert.ToString(ConfigurationManager.AppSettings["MainTitle"]);


        #region 因数初始化
        public static double hsy = Convert.ToDouble(ConfigurationManager.AppSettings["hs_y"] ?? "1");
        public static double ycy = Convert.ToDouble(ConfigurationManager.AppSettings["yc_y"] ?? "1");
        public static double cpy = Convert.ToDouble(ConfigurationManager.AppSettings["cp_y"] ?? "1");
        public static double cmvy = Convert.ToDouble(ConfigurationManager.AppSettings["cmv_y"] ?? "1");
        #endregion

        //线宽设置
        public static float SlineW = Convert.ToSingle(ConfigurationManager.AppSettings["SPCLineW"] ?? "2");
        public static float HlineW = Convert.ToSingle(ConfigurationManager.AppSettings["HISLineW"] ?? "2");
        //错误日志
        public static void LogError(string txt)
        {
            DateTime dt = DateTime.Now;
            string sCurDate = dt.ToString("yyyyMMdd");
            string logpath = System.Windows.Forms.Application.StartupPath + @"\" + sCurDate + ".log";
            FileInfo file = new FileInfo(logpath);
            if (file.Exists && file.Length/1024 > 50000)
            {
                file.Delete();
            }
            FileStream fs = new FileStream(logpath, FileMode.Append, System.IO.FileAccess.Write, FileShare.ReadWrite);
            try
            {
                StreamWriter swWrite = new StreamWriter(fs);
                swWrite.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss"));
                swWrite.WriteLine("     异常：" + txt);
                swWrite.WriteLine("\r\n\r\n");
                swWrite.Flush();
                swWrite.Dispose();
            }
            finally
            {
                fs.Close();
            }
        }

        public static void DelErrLog()
        {
            DateTime dt = DateTime.Now.AddMonths(-1);
            string fileStart = dt.ToString("yyyyMM");
            foreach (string file in Directory.GetFiles(System.Windows.Forms.Application.StartupPath, fileStart + "*.log"))
            {
                File.Delete(file);
            }
        }

        public static void DelAErrLog(string fileName)
        {

            File.Delete(fileName);

        }

        
        public CommonClass()
        {

        }

        /// <summary>
        /// 监控参数——数据库更新
        /// </summary>
        /// <param name="pmmr">设备测量参数实体</param>
        /// <param name="stmr">设备总览参数实体</param>
        /// <returns></returns>
        public bool DataToDB(ht_pmmr pmmr, ht_stmr stmr)
        {
            ht_pmmrTableAdapter pmmrTa = new ht_pmmrTableAdapter(); ;
            ht_stmrTableAdapter stmrTa = new ht_stmrTableAdapter(); ;
            ht_pmlogTableAdapter pmLgTa = new ht_pmlogTableAdapter(); ;
            pmLgTa.InsertLog(pmmr.devId, pmmr.devNm, pmmr.gyid, pmmr.st, pmmr.CMV, pmmr.hswdSet,
                pmmr.hswdCur, pmmr.ycwdSer, pmmr.ycwdCur, pmmr.cpSet, pmmr.cpCur,pmmr.dt,pmmr.tlSet,pmmr.tlCur,pmmr.RxSet,pmmr.RxCur);

            int pmmrUD = pmmrTa.UpdateMr(pmmr.gyid, pmmr.st, pmmr.CMV, pmmr.hswdSet,
                   pmmr.hswdCur, pmmr.ycwdSer, pmmr.ycwdCur, pmmr.cpSet, pmmr.cpCur,pmmr.tlSet,pmmr.tlCur,pmmr.RxSet,pmmr.RxCur,pmmr.devId);
            
            int stmrUD = (int)stmrTa.UpdateStmr(stmr.powerSt, stmr.onRt, stmr.offRt, 
                stmr.OEE, stmr.prdbn, stmr.chktms, stmr.dt, stmr.devId);
           
            if (pmmrUD < 1)
            {
                pmmrTa.InsertPmmr(pmmr.devId, pmmr.devNm, pmmr.gyid, pmmr.st,
                    pmmr.CMV, pmmr.hswdSet, pmmr.hswdCur, pmmr.ycwdSer, pmmr.ycwdCur, pmmr.cpSet, pmmr.cpCur,pmmr.tlSet,pmmr.tlCur,pmmr.RxSet,pmmr.RxCur);
            }
            if (stmrUD < 1)
            {
                stmrTa.InsertStmr(stmr.devId, stmr.devNm, stmr.powerSt, stmr.onRt, stmr.offRt, stmr.OEE, stmr.prdbn, stmr.chktms, stmr.dt);
            }
            return true;
        }
        
    }
}
